import { electronAPI } from '@electron-toolkit/preload'
import { contextBridge, ipcRenderer } from 'electron' // 添加 ipcRenderer 和 contextBridge 导入

// Custom APIs for renderer
const api = {
  getVersion: () => process.env.npm_package_version, // 获取版本号
  getProcess: () => process,
  getProcessMemoryInfo: () => process.getSystemMemoryInfo(),

  // 流配置相关API
  // 添加流配置
  // addStreamConfig: (config) => ipcRenderer.send('add-stream-config', config),
  addStreamConfig: (config) => ipcRenderer.send('addStreamConfig', config),
  // 加载流配置列表
  loadStreamsConfig: () => ipcRenderer.send('loadStreamsConfig'),
  // 删除流配置
  deleteStreamConfig: (id) => ipcRenderer.send('delete-stream-config', id),
  // 切换流状态
  // toggleStreamStatus: (id) => ipcRenderer.send('toggle-stream-status', id),
  startStream: (id) => ipcRenderer.send('startStream', id),
  stopStreaming: (id) => ipcRenderer.send('stopStreaming', id),
  // 停止所有转码服务
  stopAllStreaming: () => ipcRenderer.send('stopAllStreaming'),

  // 事件监听器
  onStreamsConfigList: (callback) => ipcRenderer.on('streams-config-list', callback),
  // onAppLog: (callback) => ipcRenderer.on('addLog', callback),
  addLog: (callback) => ipcRenderer.on('addLog', callback),

  // 添加获取应用配置的方法
  // getAppConfig: () => ipcRenderer.send('get-app-config'),
  // onAppConfig: (callback) => ipcRenderer.on('app-config', callback),

  // 移除事件监听器
  removeAllListeners: (channel) => ipcRenderer.removeAllListeners(channel),

  // 导入预置配置
  importPresetConfig: () => ipcRenderer.send('import-preset-config'),
  // 重置为预置配置
  resetToPresetConfig: () => ipcRenderer.send('reset-to-preset-config')
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}

console.log('-- preload done --')
